package com.yang.design.create.singletonPattern;


import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

/**
 * 饿汉式-单例
 */
public class Single01 {

    private final static Single01 instance = new Single01();

    private Single01(){
        if (instance != null){
            throw new RuntimeException("单例模式,不允许创建对象");
        }
    }

    public static Single01 getInstance(){
        return instance;
    }

    public static void main(String[] args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
        Single01 instance1 = Single01.getInstance();
        Single01 instance2 = Single01.getInstance();
        System.out.println(instance1);
        System.out.println(instance2);

        Class<Single01> clz = Single01.class;
        Constructor<Single01> constructor = clz.getDeclaredConstructor(null);
        constructor.setAccessible(true);
        Single01 instance3 = constructor.newInstance();
        Single01 instance4 = constructor.newInstance();

        System.out.println(instance3);
        System.out.println(instance4);
    }
}
